package com.stars.home.utils

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.*
import com.qmuiteam.qmui.kotlin.onClick
import com.qmuiteam.qmui.layout.QMUIFrameLayout
import com.qmuiteam.qmui.skin.QMUISkinValueBuilder
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.widget.popup.QMUIPopup
import com.qmuiteam.qmui.widget.popup.QMUIPopups
import com.stars.home.R
import com.stars.home.inte.ChoiceData

/**
 *
 *on 2020/9/21
 */
object  IdentityDialogUtils {

    fun shoChooseIdentity(context:Context,lei:String, view: View, choiceData: ChoiceData ){

        var mNormalPopup = QMUIPopups.fullScreenPopup(context)
        val builder: QMUISkinValueBuilder = QMUISkinValueBuilder.acquire()
        val frameLayout = QMUIFrameLayout(context)

        //自定义布局
        val layoutInflater = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        var layout = layoutInflater.inflate(R.layout.item_identity, null)
        val flOwner:FrameLayout=layout.findViewById(R.id.fl_owner)
        val flDriver:FrameLayout=layout.findViewById(R.id.fl_driver)

        val ivOwner: ImageView =layout.findViewById(R.id.iv_owner)
        val ivDriver:ImageView=layout.findViewById(R.id.iv_driver)

        val butSure: TextView = layout.findViewById(R.id.tv_sure)

        var chosetype=0

        if(lei == "2"){
            chosetype=2
            ivOwner.setImageResource(R.drawable.ic_identity_on)
            ivDriver.setImageResource(R.drawable.ic_identity_off)
        }else{
            chosetype=1
            ivOwner.setImageResource(R.drawable.ic_identity_off)
            ivDriver.setImageResource(R.drawable.ic_identity_on)
        }

        flOwner.onClick {
            chosetype=2
            ivOwner.setImageResource(R.drawable.ic_identity_on)
            ivDriver.setImageResource(R.drawable.ic_identity_off)

        }
        flDriver.onClick {
            chosetype=1
            ivOwner.setImageResource(R.drawable.ic_identity_off)
            ivDriver.setImageResource(R.drawable.ic_identity_on)
        }


        butSure.onClick {

            if(chosetype==0){
                Toast.makeText(context,"请选择您的身份！",Toast.LENGTH_SHORT).show()

                return@onClick
            }

            choiceData?.onRestuse(chosetype.toString())

            mNormalPopup.dismiss()
        }


        builder.release();

       // val size= QMUIDisplayHelper.getScreenWidth(context)-QMUIDisplayHelper.dp2px(context, 54)

        val size= QMUIDisplayHelper.getScreenWidth(context)


        val lp: FrameLayout.LayoutParams = FrameLayout.LayoutParams(size,
                FrameLayout.LayoutParams.WRAP_CONTENT)
        frameLayout.addView(layout, lp)

        mNormalPopup.addView(frameLayout)
        mNormalPopup.onDismiss() {
        }

        mNormalPopup.dismissIfOutsideTouch(false)
        mNormalPopup.animStyle(QMUIPopup.ANIM_GROW_FROM_CENTER)
        mNormalPopup.show(view)


    }
}
